package com.ujs.edu.demo.controller;

import com.ujs.edu.demo.dao.BcAdminMapper;
import com.ujs.edu.demo.dataobject.BcAdmin;
import com.ujs.edu.demo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.view.RedirectView;

import java.util.List;

@Controller
public class AdminController {

    @Autowired
    private BcAdminMapper bcAdminMapper;

    //加载管理员控制界面
    @RequestMapping(path = "admin")
    public ModelAndView toadmin(){
        ModelAndView view=new ModelAndView();
        List<BcAdmin> bcAdmins=bcAdminMapper.selectAll();
        view.addObject("admins",bcAdmins);
        view.setViewName("admin");
        return view;
    }


//    添加管理员
    @RequestMapping(path = "/addAdmin1")
    @ResponseBody
    public Result addAdmin(BcAdmin bcAdmin){
        Result result=new Result();
        System.out.println(bcAdmin.getUsername());
        BcAdmin bcAdmin1=bcAdminMapper.selectByMobile(bcAdmin.getMobile());
        System.out.println(bcAdmin.getRoleId());
        if (bcAdmin1!=null){
            result.setStatus(0);
            result.setMsg("已经存在");
        }else {
            bcAdminMapper.insert(bcAdmin);
            result.setStatus(1);
            result.setMsg("插入成功");
        }
        return result;
    }

    //将点击这一行的管理员的信息填入到输入框
    @RequestMapping(path = "/putInform")
    public ModelAndView putInForm(@RequestParam(value = "adminId") Integer adminId){

        ModelAndView view=new ModelAndView();

        BcAdmin bcAdmin=bcAdminMapper.selectByPrimaryKey(adminId);
        if (bcAdmin!=null){
            view.addObject("modifyAdmin",bcAdmin);
        }else {
           view.setViewName("error.html");
            return view;
        }

        view.setViewName("modifyadmin");
        return view;
    }

    //修改管理员
    @RequestMapping(path = "/modifyAdmin")
    @ResponseBody
    public  Result modifyAdmin(BcAdmin bcAdmin) throws Exception{

        Result result=new Result();

        BcAdmin bcAdmin1=bcAdminMapper.selectByMobile(bcAdmin.getMobile());
        if (bcAdmin1!=null){
            bcAdmin.setAdminId(bcAdmin1.getAdminId());
            int bc=bcAdminMapper.updateByPrimaryKey(bcAdmin);
            if (bc==1){
                result.setStatus(1);
                result.setMsg("成功");
            }else {
                result.setStatus(0);
                result.setMsg("失败");
            }
        }
        return result;
    }

    //删除管理员
    @RequestMapping(path = "deleteAdmin")
    @ResponseBody
    public Result deleteAdmin(@RequestParam("adminId") Integer adminId){
        System.out.println("进入后台");
        Result result=new Result();

        RedirectView view=new RedirectView();

        int del=bcAdminMapper.deleteByPrimaryKey(adminId);
        System.out.println(del);
        if (del==1){
            result.setMsg("成功");
            result.setStatus(1);
        }else {
            result.setStatus(0);
            result.setMsg("失败");
        }
        view.setUrl("/admin");
        return result;
    }

}
